package com.authority.controller;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.entry.AuthSysDO;
import com.authority.entry.AuthSysDomainDO;
import com.authority.service.AuthSysService;
import com.authority.vo.AuthSysVO;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * 
 * @author wb-leizhendong
 *
 */

@Controller
@RequestMapping(value = "/auth/sysManager")
public class SysManager extends BaseManager {

	@Resource
	private AuthSysService authSysService;

	/**
	 * 查询系统列表
	 * 
	 * @return
	 */
	
	@RequestMapping(value = "/queryAuthSys.json")
	@ResponseBody
	public Object doQueryAuthSys() {

		try {
			return success(authSysService.queryAuthSys());
		} catch (Exception e) {
			return fail(e);
		}

	}

	/**
	 * 根据id查询系统信息
	 * 
	 * @param id
	 * @return
	 */
	
	@RequestMapping(value = "/queryAuthSysById.json")
	@ResponseBody
	public Object doQueryAuthSysById(Long id) {

		try {
			checkParams(id);

			return success(authSysService.queryAuthSysById(id));
		} catch (Exception e) {
			return fail(e);
		}

	}

	/**
	 * 根据系统id查询域名信息
	 * 
	 * @param id
	 * @return
	 */
	
	@RequestMapping(value = "/queryAuthSysDomainBySysId.json")
	@ResponseBody
	public Object doQueryAuthSysDomainBySysId(Long id) {

		try {
			checkParams(id);

			return success(authSysService.queryAuthSysDomainBySysId(id));
		} catch (Exception e) {
			return fail(e);
		}

	}

	/**
	 * 新增系统信息
	 * 
	 * @param sysName
	 * @param sysCode
	 * @param domainInfo
	 * @return
	 */
	
	@RequestMapping(value = "/saveSys.json")
	@ResponseBody
	public Object doSaveSys(String name, String code, Integer type, String userInterface,String domainInfo) {

		try {
			AuthSysDO db = new AuthSysDO();
			db.setName(name);
			db.setCode(code);
			db.setType(type);
			db.setUserInterface(userInterface);
			db.setOperatorId(getUserId());
			db.setStatus(1);

			List<AuthSysDomainDO> list = getAuthSysDomainDOS(domainInfo);
			return success(authSysService.insertAuthSys(db, list));
		} catch (Exception e) {
			e.printStackTrace();
			return fail(e.getMessage());
		}

	}

	/**
	 * 更新系统信息
	 * 
	 * @param sysId
	 * @param sysName
	 * @param sysCode
	 * @param domainInfo
	 * @return
	 */
	
	@RequestMapping(value = "/updateSysInfo.json")
	@ResponseBody
	public Object doUpdateSysInfo(Long sysId,String name,String code,Integer type,String userInterface, String domainInfo) {

		try {
			AuthSysVO vo = authSysService.queryAuthSysById(sysId);
			vo.setCode(code);
			vo.setName(name);
			vo.setType(type);
			vo.setUserInterface(userInterface);
			vo.setOperatorId(getUserId());

			List<AuthSysDomainDO> list = getAuthSysDomainDOS(domainInfo);
			return success(authSysService.updateSysInfo(vo, list));
		} catch (Exception e) {
			return fail(e.getMessage());
		}

	}

	/**
	 * 解析domainInfo 生成域名list
	 * 
	 * @param domainInfo
	 * @return
	 * @throws Exception
	 */
	
	public List<AuthSysDomainDO> getAuthSysDomainDOS(String domainInfo) throws Exception {

		List<AuthSysDomainDO> list = Lists.newArrayList();
		String[] domainArr = domainInfo.split(";");
		Set<String> set = Sets.newHashSet();
		for (String str : domainArr) {
			AuthSysDomainDO db = new AuthSysDomainDO();
			String[] dbStr = str.split(",");
			if (!set.add(dbStr[1])) {
				throw new Exception("域名编码不能重复！！");
			}
			db.setName(dbStr[0]);
			db.setCode(dbStr[1]);
			//db.setOperatorId(getUserId());
			db.setStatus(1);
			list.add(db);
		}
		return list;

	}

}
